package com.anb.app.demo;

import com.anb.common.Log;
import com.anb.common.Utils;
import com.anb.net.*;
import com.anb.net.Error;
import com.anb.proto.Common.*;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

public class Demo {
    private static class Waiter {
        //
    }

    public static void main(String[] args) {
        Utils.parseBinary("100G10M5K1B12");
        try {
            new Demo().demo();
        } catch (Exception e) {
            Log.Get().error("demo error", e);
        }
        try {
            System.in.read();
        } catch (IOException e) {
            Log.Get().error(e);
        }
    }

    public void demo() throws Exception {
        Log.Get().info("preparing dispatcher...");
        String filename = getClass().getClassLoader().getResource("cmds.xml").getFile();
        Dispatcher dispatcher = new Dispatcher(ClassMethodCmdHandler.class, new Object[] {
                "com.anb.app.demo.handler"
        });
        dispatcher.load(new File(filename), new String[] {
                "java.util",
                "com.anb.proto.Common"
        });

        Log.Get().info("preparing runner...");
        IRunner runner = new Runner();
        new Thread(runner).start();

        Log.Get().info("preparing server a...");
        Server serverA = new Server(runner, Connection.class, dispatcher);
        serverA.start(InetAddress.getLoopbackAddress(), 9981);

        Log.Get().info("preparing server b...");
        Server server1 = new Server(runner, Connection.class, dispatcher);
        server1.start(InetAddress.getLoopbackAddress(), 9982);

        Client client = new Client(runner, Connection.class, dispatcher);
        client.start(InetAddress.getLoopbackAddress(), 9986, new Client.ConnectHandler() {
            @Override
            public void success(ConnectionBase c) {
                Connection connection = (Connection)c;
                try {
                    testConnectionAsync(connection);
                    testConnectionAsync(connection);
                    testConnection(connection);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void error(Exception e) {
                Log.Get().error("connect failed", e);
            }
        });
        /*
        */
    }

    public void testConnection(Connection connection) throws Exception {
        String str = connection.request("System.Echo", ">>>");
        Log.Get().info("sync " + str);
    }

    public void testConnectionAsync(Connection connection) throws Exception {
        connection.requestAsync("System.Echo", ">>>", new Connection.ResponseHandler<String>() {
            @Override
            public void success(String str) {
                Log.Get().info("sync " + str);
            }
            @Override
            public void error(Error.Code error, Object obj) {
            }
        });
        Person person = Person.newBuilder()
                .setId(119)
                .setName("pickman")
                .build();
        connection.requestAsync("Player.EchoPerson", person, new Connection.ResponseHandler<Person>() {
            @Override
            public void success(Person person1) {
                Log.Get().info("sync " + person1.getId() + "/" + person1.getName());
            }
            @Override
            public void error(Error.Code error, Object obj) {
            }
        });
    }
}
